package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Check;
import com.woniuxy.entity.CheckFund;
import com.woniuxy.entity.Doctor;
import com.woniuxy.entity.PatientCheckList;
import com.woniuxy.entity.dto.PatientCheckDTO;
import com.woniuxy.mapper.DoctorMapper;
import com.woniuxy.mapper.PatientChecklistMapper;
import com.woniuxy.service.IDoctorService;
import com.woniuxy.service.PatientChecklistService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.List;

@Service
public class PatientChecklistServiceImpl implements PatientChecklistService, SaasService {
    @Autowired
    PatientChecklistMapper patientChecklistMapper;

    @Autowired
    DoctorMapper doctorMapper;
    @Autowired
    IDoctorService doctorService;
    @Override
    public ResponseData<?> queryList(PageParam<PatientCheckDTO> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<PatientCheckList> patientChecklists = patientChecklistMapper.queryList(pageParam.getParam());
        PageInfo pageInfo = new PageInfo(patientChecklists);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> updateStatus(Integer id) {
        patientChecklistMapper.updateStatus("3",id);
        return new ResponseData<>().ok();
    }

    //添加病人 的检查明细
    // 异步 todo
    @Override
    public ResponseData<?> addPatientCheck(PatientCheckDTO patientCheckDTO) {
        try {
            //添加检查明细

            Check check = doctorMapper.queryCheckById(patientCheckDTO.getProjectId());
            if (check==null){
                return new ResponseData<>().fail(ResponseEnum.FAIL);
            }
            Doctor doctor = (Doctor) doctorService.queryDoctor().getData();
            //雪花id
            SnowflakeIdGenerator snow = new SnowflakeIdGenerator();
            long nextId = snow.nextId();
            //检查明细对象
            PatientCheckList patientCheckList = new PatientCheckList();
            patientCheckList.setCkNo(String.valueOf(nextId));
            patientCheckList.setCkCaseId(patientCheckDTO.getPatientId());
            //项目名
            patientCheckList.setCkProjectName(check.getPjName());
            //价格
            patientCheckList.setCkProjectPrice(check.getPjPrice());
            //开检查的医生
            patientCheckList.setCkWorkerId(doctor.getId());
            //未缴费 2
            patientCheckList.setCkStatus(CONSTANT.NOT_PAY.toString());
            //开单医生类型
            if (doctor.getDocPtId().equals(CONSTANT.IS_OUTPATIENT)) {
                patientCheckList.setCkType("0");//门诊医生 开的检查 0
            }else {
                patientCheckList.setCkType("1");//住院医生 开的检查 1
            }
            patientChecklistMapper.addPatientCheck(patientCheckList);

            //添加t_cf数据
            CheckFund checkFund = new CheckFund();
            checkFund.setCfCheckId(String.valueOf(nextId));
            checkFund.setCfPrice(check.getPjPrice());
            checkFund.setCfCaseId(patientCheckDTO.getPatientId());
            checkFund.setCfProjectName(check.getPjName());
            long cfId = snow.nextId();
            checkFund.setCfNo(String.valueOf(cfId));
            patientChecklistMapper.addCheckFund(checkFund);

            return new ResponseData<>().ok(patientCheckList);
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
